/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import event.RecoveryEvent;
import event.TrustEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.appia.core.AppiaEventException;
import net.sf.appia.core.Direction;
import net.sf.appia.core.Event;
import net.sf.appia.core.Layer;
import net.sf.appia.core.Session;
import net.sf.appia.core.events.channel.ChannelInit;
import net.sf.appia.protocols.common.RegisterSocketEvent;
import util.Constants;
import util.Debug;
import util.Process;

/**
 *
 * @author Welington
 */
public class ApplicationSession extends Session {

    private Class current = ApplicationSession.class;
    private Debug debug = Debug.getInstance();
    private ArrayList<Process> processes;
    private Process sourceProcess;

    public ApplicationSession(Layer layer) {
        super(layer);
    }

    @Override
    public void handle(Event event) {
        if (event instanceof ChannelInit) {
            handleChannelInit((ChannelInit) event);
        } else if (event instanceof RegisterSocketEvent) {
            handleRegisterSocket((RegisterSocketEvent) event);
        } else if (event instanceof TrustEvent) {
            handleTrust((TrustEvent) event);
        } 
    }

    private void handleChannelInit(ChannelInit event) {
        try {
            event.go();

            debug.print(Debug.INFO, current, "Registering socket");

            debug.print(Debug.LOG, current, "Revering process from file");
            processes = Constants.getAllProcess();
            sourceProcess = Constants.SOURCE_PROCESS;
            debug.print(Debug.LOG, current, "Current process correctly identified {" + sourceProcess.getId() + " - " + sourceProcess.getLabel() + "}");

            RegisterSocketEvent reg = new RegisterSocketEvent(event.getChannel(), Direction.DOWN, this, sourceProcess.getPort());

            try {
                reg.localHost = InetAddress.getLocalHost();
            } catch (UnknownHostException ex) {
                debug.print(Debug.LOG, current, ex.getLocalizedMessage());
            }
            reg.init();
            reg.go();
        } catch (AppiaEventException ex) {
            debug.print(Debug.INFO, current, ex.getLocalizedMessage());
        }
    }

    private void handleRegisterSocket(RegisterSocketEvent event) {
        if (!event.error) {
            debug.print(Debug.INFO, current, "Succeded");

            RecoveryEvent recoveryEvent = new RecoveryEvent();
            try {
                recoveryEvent.init();
                recoveryEvent.asyncGo(event.getChannel(), Direction.DOWN);
            } catch (AppiaEventException ex) {
                Logger.getLogger(ApplicationSession.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            debug.print(Debug.ERROR, current, "Failed - " + event.getErrorDescription());
        }
    }

    private void handleTrust(TrustEvent trustEvent) {
        System.out.println("Leader is       >>      "  +  trustEvent.getLeader());
    }
 
}